﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Vortex.Drawing {
	/// <summary>
	/// Represents per-pixel alpha test mode. Each pixel can be compared against specified alpha value. If condition is true then pixel will be written otherwise - no
	/// </summary>
	public class AlphaTest {
		private TestFunction _function;
		private byte _refValue;

		/// <summary>
		/// Initializes a new instance of the <see cref="AlphaTest"/> class.
		/// </summary>
		/// <param name="function">The comparison function.</param>
		/// <param name="refValue">The reference value.</param>
		public AlphaTest(TestFunction function, byte refValue) {
			_function = function;
			_refValue = refValue;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="AlphaTest"/> class.
		/// </summary>
		/// <param name="function">The comparison function.</param>
		/// <param name="refValue">The reference value.</param>
		public AlphaTest(TestFunction function, float refValue) : this(function, (byte)(refValue * 255.0f)) {
		}

		/// <summary>
		/// Gets the test function.
		/// </summary>
		/// <value>The test function.</value>
		public TestFunction Function {
			get { return _function; }
		}

		/// <summary>
		/// Gets the reference value for test.
		/// </summary>
		/// <value>The reference value.</value>
		public byte RefValue {
			get { return _refValue; }
		}

		#region Shortcut

		///<summary>Test is passed always, actually no test is executed, color data will be writter</summary>
		public static readonly AlphaTest Always = new AlphaTest(TestFunction.Always, 0.0f);

		///<summary>Test is never passed, so pixels color data never will be written</summary>
		public static readonly AlphaTest Never = new AlphaTest(TestFunction.Never, 0.0f);

		/// <summary>
		/// Passes alpha test if color value is equal with reference one.
		/// </summary>
		/// <param name="refValue">The reference value.</param>
		public static AlphaTest PassIfEqual(float refValue) {
			return new AlphaTest(TestFunction.Equal, refValue);
		}

		/// <summary>
		/// Passes alpha test if color value is not equal with reference one.
		/// </summary>
		/// <param name="refValue">The reference value.</param>
		public static AlphaTest PassIfNotEqual(float refValue) {
			return new AlphaTest(TestFunction.NotEqual, refValue);
		}

		/// <summary>
		/// Passes alpha test if color value is less than reference one.
		/// </summary>
		/// <param name="refValue">The reference value.</param>
		public static AlphaTest PassIfLess(float refValue) {
			return new AlphaTest(TestFunction.Less, refValue);
		}

		/// <summary>
		/// Passes alpha test if color value is less or equal than reference one.
		/// </summary>
		/// <param name="refValue">The reference value.</param>
		public static AlphaTest PassIfLessOrEqual(float refValue) {
			return new AlphaTest(TestFunction.LessOrEqual, refValue);
		}

		/// <summary>
		/// Passes alpha test if color value is greater than reference one.
		/// </summary>
		/// <param name="refValue">The reference value.</param>
		public static AlphaTest PassIfGreater(float refValue) {
			return new AlphaTest(TestFunction.Greater, refValue);
		}

		/// <summary>
		/// Passes alpha test if color value is greater or equal than reference one.
		/// </summary>
		/// <param name="refValue">The reference value.</param>
		public static AlphaTest PassIfGreaterOrEqual(float refValue) {
			return new AlphaTest(TestFunction.GreaterOrEqual, refValue);
		}

		#endregion

	}
}
